package jp.co.nes.awssupport.access.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;

import jp.co.nes.awssupport.common.base.CommonException;
import jp.co.nes.awssupport.common.bean.BlendedReservedManageBean;
import jp.co.nes.awssupport.common.dao.BaseJdbcDao;
import jp.co.nes.awssupport.common.utility.DateUtil;
import jp.co.nes.awssupport.common.utility.DbConnUtil;

/**
 * 融通リザーブド管理DAOです。
 */
public class BlendedReservedManageDao extends BaseJdbcDao<BlendedReservedManageBean> {

    /** 融通リザーブド管理を登録する際に使用するInsert文 */
    private static final String BATCH_INS = ""
            + "insert into blended_reserved_manage ("
            + "    UUID,"
            + "    BucketName,"
            + "    ObjectKey,"
            + "    RecordId,"
            + "    PricingPlanId,"
            + "    LinkedAccountId,"
            + "    UsageStartDate,"
            + "    UsageEndDate,"
            + "    UsageQuantity)"
            + " values(?, ?, ?, ?, ?, ?, ?, ?, ?)";

    /** 融通リザーブド管理を削除する際のDelete文 */
    private static final String DEL_BUCKETNAME_OBJECTKEY = ""
            + "delete from blended_reserved_manage"
            + " where BucketName = ?"
            + "   and ObjectKey = ?";

    /**
     * デフォルトコンストラクタです。
     */
    public BlendedReservedManageDao() {
    }

    /**
     * 引数のバケット名,オブジェクトキーに該当する融通リザーブド管理を削除します。
     *
     * @param bucketName
     * @param objectKey
     */
    public void delete(String bucketName, String objectKey) {
        try {
            Connection connection = DbConnUtil.getConn();

            Object[] params = new Object[2];
            params[0] = bucketName;
            params[1] = objectKey;

            QueryRunner runner = new QueryRunner();
            runner.update(connection, DEL_BUCKETNAME_OBJECTKEY, params);
        } catch (SQLException e) {
            throw new CommonException(e);
        }
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.dao.BaseJdbcDao#getBatchInsQuery()
     */
    @Override
    protected String getBatchInsQuery() {
        return BATCH_INS;
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.dao.BaseJdbcDao#toBatchInsObjectList(jp.co.nes.awssupport.common.bean.BaseBean)
     */
    @Override
    protected List<Object> toBatchInsObjectList(BlendedReservedManageBean bean) {
        List<Object> objectList = new ArrayList<>();
        objectList.add(bean.getUuid());
        objectList.add(bean.getBucketName());
        objectList.add(bean.getObjectKey());
        objectList.add(bean.getRecordId());
        objectList.add(bean.getPricingPlanId());
        objectList.add(bean.getLinkedAccountId());
        objectList.add(DateUtil.toTimestamp(bean.getUsageStartDate()));
        objectList.add(DateUtil.toTimestamp(bean.getUsageEndDate()));
        objectList.add(bean.getUsageQuantity());
        return objectList;
    }
}
